<?php
App::uses('AppController', 'Controller');
/**
 * Idiomas Controller
 *
 * @property Idioma $Idioma
 */
class IdiomasController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('trocaIdioma');
	}
	
	public function index() {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		$this->Idioma->recursive = 0;
		$this->set('idiomas', $this->paginate());
	}
	
	public function trocaIdioma($idioma_id) {
		if (!$idioma_id) {
			throw new NotFoundException(__('Idioma inválido'));
		}
		$idioma_selecionado = $this->Idioma->findById($idioma_id);
		//pr($idioma_selecionado['Idioma']['codigo']);
		Configure::write('Config.language', $idioma_selecionado['Idioma']['codigo']);
		$this->Session->write('Config.language', $idioma_selecionado['Idioma']['codigo']);
		$this->redirect($this->request->referer());
	}
	
	public function ativar_desativar($idioma_id = null) {
		if(!$this->isAdmin()){
			$this->notAuth();
		}
		if (!$this->Idioma->exists($idioma_id)) {
			throw new NotFoundException(__('Idioma inválido'));
		}
		$idioma_selecionado = $this->Idioma->findById($idioma_id);
		$this->Idioma->id = $idioma_id;
		
		if ($this->Idioma->saveField('ativo', (($idioma_selecionado['Idioma']['ativo']) + 1) % 2)) {
			$this->Session->setFlash(__('Status do Idioma foi alterado'));
			$this->redirect(array('action' => 'index'));
		}
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Idioma->exists($id)) {
			throw new NotFoundException(__('Invalid idioma'));
		}
		$options = array('conditions' => array('Idioma.' . $this->Idioma->primaryKey => $id));
		$this->set('idioma', $this->Idioma->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Idioma->create();
			if ($this->Idioma->save($this->request->data)) {
				$this->Session->setFlash(__('The idioma has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The idioma could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Idioma->exists($id)) {
			throw new NotFoundException(__('Invalid idioma'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			pr($this->request->data);return;
			if ($this->Idioma->save($this->request->data)) {
				$this->Session->setFlash(__('The idioma has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The idioma could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Idioma.' . $this->Idioma->primaryKey => $id));
			$this->request->data = $this->Idioma->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Idioma->id = $id;
		if (!$this->Idioma->exists()) {
			throw new NotFoundException(__('Invalid idioma'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Idioma->delete()) {
			$this->Session->setFlash(__('Idioma deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Idioma was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
